package com.thinkfit.jy.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.common.lib.constant.Constants;
import com.common.lib.manager.DataManager;
import com.thinkfit.jy.R;
import com.thinkfit.jy.activity.ApplicationActivity;
import com.thinkfit.jy.activity.FreeMovementActivity;
import com.thinkfit.jy.activity.LoginActivity;
import com.thinkfit.jy.activity.SettingActivity;
import com.thinkfit.jy.activity.TVActivity;
import com.thinkfit.jy.activity.UserProfileActivity;
import com.thinkfit.jy.activity.VideoActivity;
import com.thinkfit.jy.adapter.BMWMainAdapter;
import com.thinkfit.jy.bean.MainDataBean;
import com.thinkfit.jy.serialport.SerialPortManager;
import com.thinkfit.jy.utils.BluetoothOrder;
import com.thinkfit.jy.utils.MotionDataUtils;
import com.thinkfit.jy.view.LoopViewLayout;
import com.thinkfit.jy.view.OnItemSelectedListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 宝马风格主题首页菜单界面
 */
public class HomeBMWFragment extends HomeADFragment {


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home_bmw;
    }

    protected void init() {
        setViewsOnClickListener(R.id.ivSelectLogo);
        LoopViewLayout loopViewLayout = getView().findViewById(R.id.loopViewLayout);
        BMWMainAdapter adapter = new BMWMainAdapter();
        loopViewLayout.setAdapter(adapter);
        List<MainDataBean> list = new ArrayList<>();
        list.add(new MainDataBean(getString(R.string.app_user_center),
                R.drawable.app_user_center_bmw_t, R.drawable.app_user_center_bmw));
        list.add(new MainDataBean(getString(R.string.app_video_course),
                R.drawable.app_video_course_bmw_t, R.drawable.app_video_course_bmw));
        list.add(new MainDataBean(getString(R.string.app_live_video),
                R.drawable.app_live_video_bmw_t, R.drawable.app_live_video_bmw));
        list.add(new MainDataBean(getString(R.string.app_3d_course),
                R.drawable.app_3d_course_bmw_t, R.drawable.app_3d_course_bmw));
        list.add(new MainDataBean(getString(R.string.app_display),
                R.drawable.app_display_bmw_t, R.drawable.app_display_bmw));
        list.add(new MainDataBean(getString(R.string.app_setting),
                R.drawable.app_setting_bmw_t, R.drawable.app_setting_bmw));
        list.add(new MainDataBean(getString(R.string.app_tv),
                R.drawable.app_tv_bmw_t, R.drawable.app_tv_bmw));
        list.add(new MainDataBean(getString(R.string.app_application_manage),
                R.drawable.app_application_bmw_t, R.drawable.app_application_bmw));
        list.add(new MainDataBean(getString(R.string.app_training_target),
                R.drawable.app_target_training_bmw_t, R.drawable.app_target_training_bmw));
        list.add(new MainDataBean(getString(R.string.app_fast_start),
                R.drawable.app_fast_start_bmw_t, R.drawable.app_fast_start_bmw));
        adapter.setDatas(list);
        int width = getDisplayMetrics().widthPixels;
        int height = getDisplayMetrics().heightPixels;
        int heightTop = height / 8;
        String substring = android.os.Build.MODEL.substring(2, 5);
        if (substring.equals("101")) {
            heightTop = height / 6;
        } else if (substring.equals("156")) {
            heightTop = height / 7;
        }
        ImageView ivSelectBg = getView().findViewById(R.id.ivSelectBg);
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) ivSelectBg.getLayoutParams();
        lp.setMargins(0, heightTop, 0, 0);
        loopViewLayout
                .setR(width / 3)//设置R的大小
                .setMultiple(2f)
                .setAutoRotation(false)//是否自动切换
                .setAutoScrollDirection(LoopViewLayout.AutoScrollDirection.LEFT)
                .setAutoRotationTime(3000);//自动切换的时间  单位毫秒
        loopViewLayout.setLoopRotationX(12);
        loopViewLayout.notifyDataSetChanged();
        ImageView ivSelectLogo = getView().findViewById(R.id.ivSelectLogo);
        ivSelectLogo.setTag(0);
        loopViewLayout.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void selected(int item, View view) {
                ivSelectLogo.setTag(item);
                setImage(R.id.ivSelectLogo, list.get(item).getImg());
            }
        });
        ivSelectLogo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int tag = (int) v.getTag();
                switch (tag) {
                    case 0:
                        if (DataManager.Companion.getInstance().getMyInfo() == null) {
                            showToast(R.string.app_please_login_first);
                            openActivity(LoginActivity.class);
                            return;
                        }
                        openActivity(UserProfileActivity.class);
                        break;
                    case 1:
                        Bundle bundle = new Bundle();
                        bundle.putInt(Constants.BUNDLE_EXTRA, 1);
                        openActivity(VideoActivity.class, bundle);
                        break;
                    case 2:
                        bundle = new Bundle();
                        bundle.putInt(Constants.BUNDLE_EXTRA, 4);
                        openActivity(VideoActivity.class, bundle);
                        break;
                    case 3:
                        break;
                    case 4:
                        try {
                            Intent intent = getActivity().getPackageManager().
                                    getLaunchIntentForPackage("com.rockchip.wfd");
                            startActivity(intent);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                    case 5:
                        openActivity(SettingActivity.class);
                        break;
                    case 6:
                        openActivity(TVActivity.class);
                        break;
                    case 7:
                        openActivity(ApplicationActivity.class);
                        break;
                    case 8:
                        bundle = new Bundle();
                        bundle.putInt(Constants.BUNDLE_EXTRA, 0);
                        openActivity(FreeMovementActivity.class, bundle);
                        break;
                    case 9:
                        if (!SerialPortManager.getInstance().isConnected()&&!BluetoothOrder.getInstance().isConnect()) {
                            showToast(R.string.app_device_connect);
                            return;
                        }
                        if (MotionDataUtils.getInstance().isRun()) {
                            SerialPortManager.getInstance().stopRun();
                        } else {
                            SerialPortManager.getInstance().startRun();
                            if (DataManager.Companion.getInstance().isLogin()) {
                                bundle = new Bundle();
                                bundle.putInt(Constants.BUNDLE_EXTRA, 1);
                                openActivity(FreeMovementActivity.class, bundle);
                            }
                        }
                        break;
                }
            }
        });
    }
}
